import request from '../request.ts'

// RBAC 教师管理API

// 创建教师用户（专用接口）
export const createTeacherUser = (data) => {
  return request({
    url: '/rbac/create-teacher',
    method: 'post',
    data
  })
}

// 创建教师（别名）
export const createTeacher = createTeacherUser

// 获取用户列表
export const getUsers = (params) => {
  return request({
    url: '/rbac/users',
    method: 'get',
    params
  })
}

// 获取教师列表（别名）
export const getTeachers = getUsers

// 创建用户
export const createUser = (data) => {
  return request({
    url: '/rbac/users',
    method: 'post',
    data
  })
}

// 获取用户详情
export const getUser = (userId) => {
  return request({
    url: `/rbac/users/${userId}`,
    method: 'get'
  })
}

// 更新用户信息
export const updateUser = (userId, data) => {
  return request({
    url: `/rbac/users/${userId}`,
    method: 'put',
    data
  })
}

// 更新教师（别名）
export const updateTeacher = updateUser

// 删除用户
export const deleteUser = (userId) => {
  return request({
    url: `/rbac/users/${userId}`,
    method: 'delete'
  })
}

// 删除教师（别名）
export const deleteTeacher = deleteUser

// 获取用户角色
export const getUserRoles = (userId) => {
  return request({
    url: `/rbac/users/${userId}/roles`,
    method: 'get'
  })
}

// 分配角色给用户
export const assignRoleToUser = (userId, roleId) => {
  return request({
    url: `/rbac/users/${userId}/roles/${roleId}`,
    method: 'post'
  })
}

// 移除用户角色
export const removeRoleFromUser = (userId, roleId) => {
  return request({
    url: `/rbac/users/${userId}/roles/${roleId}`,
    method: 'delete'
  })
}

// 获取教师权限（别名）
export const getTeacherPermissions = (userId) => {
  return request({
    url: `/rbac/users/${userId}/permissions`,
    method: 'get'
  })
}

// 批量删除教师
export const batchDeleteTeachers = (userIds) => {
  return request({
    url: '/rbac/users/batch',
    method: 'delete',
    data: { userIds }
  })
}

// 重置教师密码
export const resetTeacherPassword = (userId) => {
  return request({
    url: `/rbac/users/${userId}/reset-password`,
    method: 'post'
  })
}

// 切换教师状态
export const toggleTeacherStatus = (userId) => {
  return request({
    url: `/rbac/users/${userId}/toggle-status`,
    method: 'post'
  })
}

// 更新教师权限
export const updateTeacherPermissions = (userId, permissions) => {
  return request({
    url: `/rbac/users/${userId}/permissions`,
    method: 'put',
    data: { permissions }
  })
}

// 导入教师
export const importTeachers = (file) => {
  const formData = new FormData()
  formData.append('file', file)
  return request({
    url: '/rbac/teachers/import',
    method: 'post',
    data: formData,
    headers: {
      'Content-Type': 'multipart/form-data'
    }
  })
}

// 导出教师
export const exportTeachers = (params) => {
  return request({
    url: '/rbac/teachers/export',
    method: 'get',
    params,
    responseType: 'blob'
  })
}